﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Text;

using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.Common.Utilities;
using IBatisNet.DataAccess;
using IBatisNet.DataAccess.Configuration;
using IBatisNet.DataAccess.Interfaces;
using IBatisNet.Common.Logging;

//using MT.eVotePRS.Persistence.Interface;
//using MT.eVotePRS.Persistence.MapperDao;
using MT.eVotePRS.Model;

namespace MT.eVotePRS.Service
{
    public class HandsetService
    {
        public static SqlMapper _getsqlmapper = null;
        // private IDaoManager _getdaoManager = null;
        // private IHandsetMapDao _gethandsetMapDao = null;
        public ISqlMapper _getsqlManager = null;

        public HandsetService()
        {
            DomSqlMapBuilder getdombuilder = new DomSqlMapBuilder();
            if(getdombuilder!=null)
                // HandsetService._getsqlmapper = getdombuilder.Configure() as SqlMapper;
                _getsqlmapper = getdombuilder.Configure() as SqlMapper;

            _getsqlManager = Mapper.Instance();
        }

        public object InsertHandset(Handsets handset)
        {
            ISqlMapper _getsqlManager = null;
            DomSqlMapBuilder getdombuilder = new DomSqlMapBuilder();

            if (getdombuilder != null)
                // HandsetService._getsqlmapper = getdombuilder.Configure() as SqlMapper;
                _getsqlmapper = getdombuilder.Configure() as SqlMapper;
            _getsqlManager = Mapper.Instance();

            if (_getsqlManager != null)
            {
               return _getsqlManager.Insert("InsertHandset", handset);
            }
            return null;
        }

        public string DeleteHandsetById(int handsetId)
        {
            if (_getsqlManager != null)
            {
                _getsqlManager.BeginTransaction();
                try
                {
                    int getcounter = Mapper.Instance().Delete("DeleteHandset", handsetId);
                    _getsqlManager.CommitTransaction();
                    return getcounter.ToString();
                }
                catch
                {
                    _getsqlManager.RollBackTransaction();
                    throw;
                }
            }
            return "";
        }

        public string UpdateHandsetById(Handsets handset)
        {
            string getresult = string.Empty;
            if (_getsqlManager == null)
                _getsqlManager = Mapper.Instance();
            else
            {
                _getsqlManager.BeginTransaction();
                try
                {
                    getresult = _getsqlManager.Update("UpdateHandset", handset).ToString();
                    _getsqlManager.CommitTransaction();
                }
                catch
                {
                    _getsqlManager.RollBackTransaction();
                    throw;
                }
            }
            return getresult;
        }
        
        public Handsets GetHandsetByMac(string mac)
        {
            Handsets handset = null;
            if (_getsqlManager != null)
            {
                handset = _getsqlManager.QueryForObject<Handsets>("GetHandsetByMac", mac);
            }
            return handset;
        }

        public List<Handsets> GetAllHandsetList()
        {
            if (_getsqlManager != null)
            {
                List<Handsets> getResultList = new List<Handsets>();
                getResultList = _getsqlManager.QueryForList<Handsets>("SelectAllHandsets", null).ToList<Handsets>();
                return getResultList;
            }
            return null;
        }
    }
}
